package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.entity.Ideal;
import com.util.DBHelp;
import com.util.RowMapper;

public class IdealDao {
	
	DBHelp<Ideal> help=new DBHelp<Ideal>();

	public void add(Ideal ideal) {
		String sql="INSERT INTO `t_idea`(`id`,`title`,`content`,`createtime`,`employeeid`,`projectid`) VALUES(?,?,?,?,?,?)";
		help.executeSQL(sql, ideal.getId(),ideal.getTitle(),ideal.getContent(),ideal.getCreatetime(),ideal.getEmployeeid(),ideal.getProjectid());
	}

	public List<Ideal> getByPid(String id) {
		String sql="SELECT `id`,`title`,`content`,`createtime`,`employeeid`,`projectid` FROM `t_idea` WHERE `projectid`=? ORDER BY `createtime` DESC";
		return help.executeQueryForList(sql, new Mapper(), id);
	}
	
	public Ideal getById(String id) {
		String sql="SELECT `id`,`title`,`content`,`createtime`,`employeeid`,`projectid` FROM `t_idea` WHERE id=?";
		return help.executeQueryForObject(sql, new Mapper(), id);
	}
	
	public void update(Ideal ideal) {
		String sql="UPDATE `t_idea` SET `title`=?,`content`=? WHERE id=?";
		help.executeSQL(sql, ideal.getTitle(),ideal.getContent(),ideal.getId());
	}
	
	public void delete(String id) {
		String sql="DELETE FROM `t_idea` WHERE `id`=?";
		help.executeSQL(sql, id);
	}
	
	
	private class Mapper implements RowMapper<Ideal>{

		public Ideal mapperRow(ResultSet rs) throws SQLException {
			Ideal ideal=new Ideal();
			ideal.setContent(rs.getString("content"));
			ideal.setCreatetime(rs.getString("createtime"));
			ideal.setEmployeeid(rs.getString("employeeid"));
			ideal.setProjectid(rs.getString("projectid"));
			ideal.setTitle(rs.getString("title"));
			ideal.setId(rs.getString("id"));
			
			return ideal;
		}
		
	}

}
